<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>Markdown 处理器</span>
      </div>
      <el-form label-position="right" label-width="120px">
        <el-form-item label="MinIO 终端:">
          <el-input v-model="minioEndpoint" required></el-input>
        </el-form-item>
        <el-form-item label="访问密钥:">
          <el-input v-model="accessKey" required></el-input>
        </el-form-item>
        <el-form-item label="密钥:">
          <el-input v-model="secretKey" required></el-input>
        </el-form-item>
        <el-form-item label="存储桶名称:">
          <el-input v-model="bucketName" required></el-input>
        </el-form-item>
        <el-form-item label="Markdown 文件路径:">
          <el-input v-model="markdownFilePath" required></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="processMarkdown">处理 Markdown</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-dialog :visible.sync="dialogVisible" width="30%">
      <span>{{ message }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
    <el-dialog :visible.sync="successDialogVisible" title="成功" width="30%">
      <div>
        <span>Markdown 处理成功！</span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="successDialogVisible = false">确定</el-button>
      </span>
    </el-dialog>
    <el-dialog :visible.sync="errorDialogVisible" title="错误" width="30%">
      <div>
        <span>{{ errorMessage }}</span>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="errorDialogVisible = false">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { processMarkdown } from '@/api/system/markdown';

export default {
  data() {
    return {
      minioEndpoint: '',
      accessKey: '',
      secretKey: '',
      bucketName: '',
      markdownFilePath: '',
      message: '',
      dialogVisible: false,
      successDialogVisible: false,
      errorDialogVisible: false,
      errorMessage: ''
    };
  },
  methods: {
    async processMarkdown() {
      const data = {
        minioEndpoint: this.minioEndpoint,
        accessKey: this.accessKey,
        secretKey: this.secretKey,
        bucketName: this.bucketName,
        markdownFilePath: this.markdownFilePath
      };

      try {
        const response = await processMarkdown(data);
        this.message = response.data;
        this.successDialogVisible = true;
      } catch (error) {
        this.errorMessage = error.response ? error.response.data : '发生错误';
        this.errorDialogVisible = true;
      }
    }
  }
};
</script>

<style scoped>
.box-card {
  max-width: 600px;
  margin: 0 auto;
  margin-top: 2rem;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}
</style>
